#include <JSMN.au3>
#include <Constants.au3>
;
; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         xiuju_lan<xiuju_lan@foxitsoftware.com>
;
; Script Function:
;  自动打开workbench，根据输入的用户名，密码，

Local $iLoop = 1, $value = ""
While $iLoop = 1
    $value = InputBox("环境用户信息", "请按环境,用户名，密码输入，逗号分隔,正式环境用prod","docker,admin@fp.com,222222")
	If @error = 1 Then
		;MsgBox($MB_SYSTEMMODAL, "Error", "You pressed 'Cancel' - try again!")
		Exit 1
	Else
		; They clicked OK, but did they type the right thing?
		If StringInStr($value, ",") = 0 Then ; This is not case-sensitive, therefore AutoIt and AuToIT are acceptable values as well
			MsgBox(80, "输入错误", "请以逗号分隔输入")
		Else
			$iLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too
		EndIf
	EndIf
WEnd

; deal with the array ,to get the env ,email and password
$array = StringSplit($value, ",")
$env = $array[1]
$email = $array[2]
$pwd = $array[3]

;Get the work dir then Run cmd.exe ,excute the workbench.bat,then close the cmd console
$Var = StringSplit (@scriptdir,"\")
;msgbox(0,"",@scriptdir)
;msgbox(0,"",UBound($Var))

Run("cmd.exe")
WinWaitActive("[CLASS:ConsoleWindowClass]")

;Send($Var[1] & "{ENTER}")
;Send("cd D:\chemistry-opencmis-workbench-0.13.0-full{ENTER}")
Send("workbench.bat{ENTER}")
;Send(@scriptdir & "\workbench.bat{ENTER}")
WinWaitActive("[CLASS:SunAwtDialog]")
WinClose("[CLASS:ConsoleWindowClass]")

;Local $sString = StringReplace("This is a sentence with whitespace.", " ", "-")
$cmisUrl = "http://cmis.vagrant.foxitcloud.com:8180/cmis/atom";
$platformUrl = "http://platform.vagrant.foxitcloud.com:8003/rest/users/accessToken?email=" & $email & "&password=" & $pwd;


If $env = "prod" Then
   $cmisUrl = "http://cmis.foxitcloud.com:8180/cmis/atom"
   $platformUrl = "http://platform.foxitcloud.com/rest/users/accessToken?email=" & $email & "&password=" & $pwd
Else
   $cmisUrl = StringReplace($cmisUrl , "vagrant",$env);
   $platformUrl = StringReplace($platformUrl ,"vagrant" , $env);
EndIf

;MsgBox(0, "platformUrl", $platformUrl)
;MsgBox(0, "cmisUrl", $cmisUrl)
WinActivate("[CLASS:SunAwtDialog]")
$pos = WinGetPos("Login")

;MsgBox(0, “活动窗口状态 (X坐标,Y坐标,宽度,高度):”, $size[0] & “, ” & $size[1] & “, ” & $size[2] & “, ” & $size[3])
;MsgBox(0, "", $pos[0]+150)
MouseClick($MOUSE_CLICK_LEFT, $pos[0]+150, $pos[1]+90, 1)

$hWnd=WinGetHandle("[ACTIVE]");$hWnd 为目标窗口句柄，这里设置的是当前活动窗口
$ret=DllCall("user32.dll","long","LoadKeyboardLayout","str","08040804","int",1+0)
DllCall("user32.dll","ptr","SendMessage","hwnd",$hWnd,"int",0x50,"int",1,"int",$ret[0])

Send($cmisUrl)

; Get the token through a request
Dim $obj = ObjCreate ("WinHttp.WinHttpRequest.5.1")
$obj.Open("GET", $platformUrl, false)
;MsgBox(0, "", $obj)
$obj.Send()

;QuickOutput("Response.html",$obj.ResponseText, 2)

Local $objJson = Jsmn_Decode($obj.ResponseText)
$token = Jsmn_ObjGet($objJson, "data")
;MsgBox(0, "", $token)

MouseClick($MOUSE_CLICK_LEFT, $pos[0]+170, $pos[1]+150, 1)
Send($token)

MouseClick($MOUSE_CLICK_LEFT, $pos[0]+350, $pos[1]+210, 1)
MouseClick($MOUSE_CLICK_LEFT, $pos[0]+330, $pos[1]+380, 1)